/*--------------------------------------------------------------------------------------*\
**
** adv_teleporter_entrance.cpp
**
** Heroes IV
** Copyright 2000, The 3DO Company
**
\*--------------------------------------------------------------------------------------*/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adv_teleporter_entrance.h"

#include "adv_actor_model.h"
#include "adv_object_type.h"
#include "adv_teleporter_exit.h"
#include "adventure_ai.h"
#include "adventure_frame.h"
#include "adventure_map.h"
#include "adventure_path_finder.h"
#include "adventure_sounds.h"
#include "army.h"
#include "bitmap_group_cache.h"
#include "dialog_teleporter_entrance.h"
#include "gateway_selection.h"
#include "music.h"
#include "object_registration.h"
#include "player.h"
#include "simple_dialog.h"
#include "stationary_adventure_object.h"
#include "table.h"
#include "table_ptr.h"

/*
** Teleporter name strings.
*/
/*----------------------------------------------------------------------------------------------------*\
**
** Teleporter entrance.
**
\*----------------------------------------------------------------------------------------------------*/
static t_object_registration<t_adv_teleporter_entrance> k_registration( k_adv_object_teleporter_entrance );

t_adv_teleporter_entrance::t_adv_teleporter_entrance (  std::string const& model_name )
          : t_gateway_base( model_name )
{
}

void t_adv_teleporter_entrance::activate_trigger( t_army* army, t_adv_map_point const& point, 
		                           t_direction direction, t_adventure_frame* frame )
{
    t_adv_teleporter_exit_vector teleporter_exits = m_map->get_teleporter_exits( get_teleporter_type() );

	if (army->get_owner()->is_computer()) 
	{
		t_adventure_path const& path = m_map->get_ai().get_current_path();
		t_adv_map_point dest_point = point + get_direction_offset( direction );

		t_gateway_base const* base = ai_select_gateway_exit( path, teleporter_exits, this, army, dest_point );
		if (base)
			teleport_army ( *base, army, frame );
		return;
	}	
    
	t_level_map_point_2d_list    exit_points;
    std::vector<std::string>     exit_names;
    int							 i;
	int							 selected_exit = 0;

	if ( teleporter_exits.empty() )
	{
		ok_dialog( get_text("denied"), true, get_text("name") );
		return;
	}
    
    for (i=0; i < (int)teleporter_exits.size(); i++)
    {
	    exit_points.push_back ( teleporter_exits[i]->get_position() );
        exit_names.push_back ( teleporter_exits[i]->get_identifier() );
    }


    // Show garrison window.
	t_counted_ptr<t_dialog_teleporter_entrance> dialog_ptr;

	dialog_ptr = new t_dialog_teleporter_entrance( frame );
	dialog_ptr->init_dialog( frame, exit_points, exit_names, selected_exit, get_text ("Name"), get_text ("Initial") );
	t_sound_cache music = get_music_playing();
	stop_music();
	get_dialog_sound( k_dialog_sound_portal )->play( get_sound_volume() );
	dialog_ptr->run_modal();
	play_music( music );

    selected_exit = (dialog_ptr->get_selected_exit());

    if (selected_exit == -1)
        return;

    /*
    ** Teleport army.
	** Do NOT allow trades; this would allow a player to get their army back out of a one-way
	** exit
    */
    teleport_army ( *teleporter_exits[ selected_exit ], army, frame, false );
}


void t_adv_teleporter_entrance::pathing_destination_query( t_adventure_path_point const& source, 
														   t_adventure_path_finder & path_finder) const
{
    t_adv_teleporter_exit_vector teleporter_exits = m_map->get_teleporter_exits( get_teleporter_type() );

    for (int i=0; i < (int)teleporter_exits.size(); i++)
    {
		t_adv_map_point teleporter_exit;
		if ( teleporter_exits[i]->find_adjacent_space( teleporter_exit, *path_finder.get_army() ) )
		{
			teleporter_exit.on_bridge = false;

			path_finder.push( teleporter_exit, source, 0 );
		}
    } 
}

